package com.hare.common.result;


import com.hare.common.global.enums.ResultCode;

/**
 * @program: Hare
 * @description: 统一结果处理工具
 * @author: Hare
 * @create: 2022-06-22 13:21
 * @Version 6.0.1
 **/
public class R<T> implements IResult {

    /**
     * 状态码
     */
    public Integer code;

    /**
     * 返回信息
     */
    public String message;

    /**
     * 返回数据
     */
    public T data;


    public static <T> R<T> success(T data) {
        return success(ResultCode.SUCCESS, data);
    }

    public static <T> R<T> success(ResultCode resultCode) {
        return new R<>(ResultCode.SUCCESS.getCode(), null, null);
    }


    public static <T> R<T> success(ResultCode resultCode, T data) {
        return new R<>(resultCode.getCode(), resultCode.getMessage(), data);
    }

    public static <T> R<T> successOk() {
        return success(ResultCode.SUCCESS);
    }

    public static <T> R<T> successOk(int rows) {
        return rows > 0 ? success(ResultCode.SUCCESS) : error("操作失败");
    }


    public static <T> R<T> error(String message) {
        return error(message, ResultCode.ERROR.getCode());
    }

    public static <T> R<T> error(String message, Integer code) {
        return new R<>(code, message, null);
    }


    @Override
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public boolean isSuccess() {
        return code == 00000;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public R() {
    }

    public R(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public R(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
